package cn.fl.institution.controller;

import cmm.comm.facade.framework.api.ISysCdMapApiService;
import cmm.comm.facade.framework.condition.SysCdMapDQueryCondition;
import cmm.comm.facade.framework.dto.SysCdMapDDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.customer.service.ICstNpBscInfoService;
import cn.fl.customer.vo.CstNpBscInfoVO;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.institution.api.IShnsbanksInsApiService;
import cn.fl.institution.vo.BankInfo;
import cn.fl.project.condition.PrjCstAccInfoQueryCondition;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjCstAccInfoService;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjCstAccInfoVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fl.constant.facade.framework.constant.SystemConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import pam.srcbank.facade.dto.TiedCardListDTO;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 项目基础信息表 控制层 上海农商银行
 */
@Controller
@RequestMapping("/institution/shnsbank")
public class PrjBscInfoInsShnsbankController {

    private static final Logger logger = LoggerFactory.getLogger(PrjBscInfoInsShnsbankController.class);

    @Autowired
    private ICstNpBscInfoService cstNpBscInfoService; // 自然人信息

    @Autowired
    private IPrjCstAccInfoService prjCstAccInfoService;//项目客户基础信息表

    @Autowired
    private IPrjBscInfoService prjBscInfoService; // 项目基础信息表

    @Autowired
    private IShnsbanksInsApiService srcbankApiService;


    @Autowired
    private IShnsbanksInsApiService shnsbanksInsApiService;

    /**
     * 保存 项目客户信息
     */
    @RequestMapping(value = "/saveForShnsbank.do", method = RequestMethod.POST)
    @ResponseBody
    public DataResultVO saveForShnsbank(HttpServletRequest request) throws BaseException {
        StopWatch stopWatch = new StopWatch("上农商银行信息采集保存接口计时统计");
        stopWatch.start();
        BaseLogUtils.info(logger, "saveForShnsbank", "修改项目客户账户信息及校验入参"+ JSON.toJSONString(request.getParameterMap()), CurrentThreadContext.getCurrentUserName());
        String cstAccInfoId = request.getParameter("cstAccInfoId");//项目客户信息表id
        String insCode = request.getParameter("insCode");//机构code
        String prjId = request.getParameter("prjId");//项目id
        String cstId = request.getParameter("cstId");//客户id
        String cstNm = request.getParameter("cstNm");//客户姓名
        String bkNm = request.getParameter("bkNm");//银行名称
        String bkCd = request.getParameter("bkCd");//银行代码
        String accNo = request.getParameter("accNo");//银行账号
        String mp = request.getParameter("mp");//手机号
        String nation = request.getParameter("nation");//民族
        String certNo = request.getParameter("certNo");//身份证
        String certValidSDt = request.getParameter("certValidSDt");//身份证有效起始时间
        String certValidEDt = request.getParameter("certValidEDt");//身份证有效结束时间
        String postProvince = request.getParameter("rgnPrCd2");//居住地省
        String postCity = request.getParameter("rgnCyCd2");//居住地市
        DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, "保存成功");
        try {
            if (StringUtils.isEmpty(prjId)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "项目id为空!");
            }
            if (StringUtils.isEmpty(cstId)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户id为空!");
            }
            if (StringUtils.isEmpty(cstNm)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户名称为空!");
            }
            if(StringUtils.isEmpty(bkNm) || StringUtils.isEmpty(bkCd)){
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护开户银行信息!");
            }
            if(StringUtils.isEmpty(nation)){
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护民族!");
            }
            if(StringUtils.isEmpty(accNo)){
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护银行卡号!");
            }
            if(StringUtils.isEmpty(mp)){
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护银行预留手机号!");
            }
            if(StringUtils.isEmpty(postProvince)){
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护居住地-省");
            }
            if(StringUtils.isEmpty(postCity)){
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护居住地-市!");
            }
            if(StringUtils.isBlank(certValidSDt)){
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护证件有效起始日期!");
            }
            if(StringUtils.isBlank(certValidEDt)){
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护证件有效终止日期!");
            }
            if (!certValidEDt.equals(PropertiesCacheUtils.getProperty(SystemConstant.CHECK_DATE_LONG))) {
                Date startDate = DateUtils.formatStringToDate(certValidSDt, "yyyy-MM-dd");
                Date endDate = DateUtils.formatStringToDate(certValidEDt, "yyyy-MM-dd");
                if (DateUtils.equals(startDate, endDate) || DateUtils.after(endDate, startDate)) {
                    return new DataResultVO(BaseConstant.IS_YESNO_NO, "证件有效起始日期 不能大于等于 证件有效终止日期！");
                }
            }
            // 修改居住地-省/市,证件有效期-始/终,民族
            CstNpBscInfoVO bscInfoVO = this.cstNpBscInfoService.selectById(Long.valueOf(cstId));
            CstNpBscInfoVO cstNpBscInfoVO = new CstNpBscInfoVO();
            if(null != bscInfoVO) {
                boolean flag = false;
                cstNpBscInfoVO.setId(bscInfoVO.getId());
                if (!Objects.equals(bscInfoVO.getNation(), nation)){
                    cstNpBscInfoVO.setNation(nation);
                    flag = true;
                }
                if (!Objects.equals(bscInfoVO.getCertValidSDt(), certValidSDt)) {
                    cstNpBscInfoVO.setCertValidSDt(certValidSDt);
                    flag = true;
                }
                if (!Objects.equals(bscInfoVO.getCertValidEDt(), certValidEDt)) {
                    cstNpBscInfoVO.setCertValidEDt(certValidEDt);
                    flag = true;
                }
                if (!Objects.equals(bscInfoVO.getPostProvince(), postProvince) || !Objects.equals(bscInfoVO.getPostCity(), postCity)) {
                    cstNpBscInfoVO.setPostProvince(postProvince);
                    cstNpBscInfoVO.setPostCity(postCity);
                    flag = true;
                }
                if (flag) {
                    cstNpBscInfoVO.setMdfTm(new Date());
                    cstNpBscInfoVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
                    BaseLogUtils.info(logger, "saveForShnsbank", "修改自然人信息cstNpBscInfoVO"+ JSON.toJSONString(cstNpBscInfoVO), CurrentThreadContext.getCurrentUserName());
                    boolean modify = this.cstNpBscInfoService.modify(cstNpBscInfoVO);
                    BaseLogUtils.info(logger, "saveForShnsbank", "修改自然人信息modify="+ modify, CurrentThreadContext.getCurrentUserName());
                }
            }

            PrjCstAccInfoQueryCondition prjCstAccInfoQueryCondition = new PrjCstAccInfoQueryCondition();
            prjCstAccInfoQueryCondition.setPrjId(Long.valueOf(prjId));
            prjCstAccInfoQueryCondition.setIsDel(BaseConstant.IS_YESNO_NO);
            prjCstAccInfoQueryCondition.setInsCode(PrjBscInfoConstant.PRJ_INS_CODE_SHNS);
            PrjCstAccInfoVO prjCstAccInfo = null;
            List<PrjCstAccInfoVO> prjCstAccInfoVOS = this.prjCstAccInfoService.searchList(prjCstAccInfoQueryCondition);
            BaseLogUtils.info(logger, "saveForShnsbank", "上海农商银行立项保存 项目基本信息： prjBscInfoVO = " + JSONObject.toJSONString(prjCstAccInfoVOS));
            PrjCstAccInfoQueryCondition condition = new PrjCstAccInfoQueryCondition();
            if (CollectionUtils.isNotEmpty(prjCstAccInfoVOS)) {
                prjCstAccInfo = prjCstAccInfoVOS.get(0);
                if (StringUtils.isNotEmpty(cstAccInfoId)) {
                    condition.setId(Long.valueOf(cstAccInfoId));
                } else if (prjCstAccInfo.getId() != null){
                    condition.setId(prjCstAccInfo.getId());
                }
            }
            condition.setPrjId(Long.valueOf(prjId));
            condition.setCstId(Long.valueOf(cstId));
            condition.setInsCode(insCode);
            condition.setCstNm(cstNm);
            condition.setAccNm(cstNm);
            condition.setBkCd(bkCd);
            condition.setBkNm(bkNm);
            condition.setAccNo(accNo.trim());
            condition.setMp(mp);
            condition.setCertNo(certNo.trim());
            condition.setCertTypCd(DictionaryConstant.CODE_CERT_TYP_CD_0);
            condition.setCertValidSDt(certValidSDt);
            condition.setCertValidEDt(certValidEDt);
            BaseLogUtils.info(logger, "saveForShnsbank", "修改项目客户账户信息,condition:"+ JSONObject.toJSONString(condition), CurrentThreadContext.getCurrentUserName());
            dataResultVO = this.prjCstAccInfoService.modifyPrjAccInfo(condition);
            if(BaseConstant.IS_YESNO_YES == dataResultVO.getSuccess()){
                Map<String,String> resultMap = (Map<String, String>) dataResultVO.getData();
                String chkRstCd = "";
                String rstDesc = "";
                String bankCardType = "";
                chkRstCd = resultMap.get("chkRstCd");
                rstDesc = resultMap.get("rstDesc");
                bankCardType = resultMap.get("bankCardType");
                //四要素结果
                if (!BaseConstant.IS_YESNO_YES_STR.equals(chkRstCd)) {
                    dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                    dataResultVO.setInfo(rstDesc);
                    return dataResultVO;
                }
                //银行卡类型
                if (!BaseConstant.IS_YESNO_NO_STR.equals(bankCardType)) {
                    dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                    dataResultVO.setInfo("不支持此类银行卡，请更换为借记卡");
                    return dataResultVO;
                }
            }
        } catch (BaseException ex) {
            dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            dataResultVO.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "saveForShnsbank", "保存项目客户账户信息及校验,prjId:" + prjId + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception e) {
            BaseLogUtils.info(logger, "saveForShnsbank", "保存失败，系统异常",CurrentThreadContext.getCurrentUserName(), e);
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "保存失败，系统异常");
        }
        stopWatch.stop();
        BaseLogUtils.info(logger, "saveForShnsbank", "保存项目客户账户信息及校验返回dataResultVO=" +
                JSON.toJSONString(dataResultVO) + "-耗时=" + stopWatch.prettyPrint(), CurrentThreadContext.getCurrentUserName());
        return dataResultVO;
    }

    /**
     * 上海农商还款卡-查看
     * @param request
     * @return
     */
    @RequestMapping(value = "/checkSHNSPrjBscInfo.do", method = { RequestMethod.GET })
    public ModelAndView checkSNPrjBscInfo(HttpServletRequest request) throws BaseException{

        String prjId = request.getParameter("prjId");
        String queryBindFlag = request.getParameter("queryBindFlag");//是否查看标识
        BaseLogUtils.newLogger("上海农商银行资管换卡信息查看===>开始:prjId={},queryBindFlag={}",prjId,queryBindFlag).info();

        ModelAndView view = new ModelAndView("institution/snbank/checkSHNSPrjBscInfo");
        BaseLogUtils.newLogger("上海农商银行根据prjId查询项目信息表Start:prjId={}",prjId).info();
        PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
        BaseLogUtils.newLogger("上海农商银行根据prjId查询项目信息表End:prjId={},prjBscInfoVO={}",prjId,JSON.toJSONString(prjBscInfoVO)).info();
        if(null != prjBscInfoVO){
            view.addObject("lsCntNo", prjBscInfoVO.getLsCntNo());
            view.addObject("cstNm", prjBscInfoVO.getCstNm());
            view.addObject("certNo", prjBscInfoVO.getCertNo());
            view.addObject("fundRasInsId", prjBscInfoVO.getFundRasInsId());

        }
        view.addObject("queryBindFlag", queryBindFlag);
        view.addObject("prjId", prjId);
        return view;
    }
    @RequestMapping(value = "/initCardList.do", method=RequestMethod.GET)
    @ResponseBody
    public Object initList(@RequestParam("prjId") Long prjId, @RequestParam("fundRasInsId") Long fundRasInsId){
        BaseLogUtils.newLogger("上海农商银行资管初始化换卡信息列表===>开始:prjId={},fundRasInsId={}",prjId,fundRasInsId).info();
        BaseLogUtils.newLogger("上海农商银行查询卡列表信息开始:prjId={},fundRasInsId={}",prjId,fundRasInsId).info();
        DataResultDTO dataResultDTO = srcbankApiService.queryTiedCardList(fundRasInsId, Long.valueOf(prjId), null);
        BaseLogUtils.newLogger("上海农商银行查询卡列表信息结束:prjId={},fundRasInsId={},queryResultData={}",prjId,fundRasInsId,JSON.toJSONString(dataResultDTO)).info();
        List<TiedCardListDTO> tiedCardList = new ArrayList<>();
        if("1".equals(dataResultDTO.getCode()) && null != dataResultDTO.getData()){
           tiedCardList.addAll((List<TiedCardListDTO>)dataResultDTO.getData());
        }
        BaseLogUtils.newLogger("上海农商银行查询卡列表信息:prjId={},tiedCardList={}",prjId,JSON.toJSONString(tiedCardList)).info();
        ListPageVO<TiedCardListDTO> listPage = new ListPageVO<TiedCardListDTO>(tiedCardList, tiedCardList.size());
        return listPage;
    }
    /**
     * 上海农商还款卡-查询是否有解绑数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryUnBindCard.do", method = { RequestMethod.GET })
    @ResponseBody
    public DataResultVO queryUnBindCard(HttpServletRequest request){
        String prjId = request.getParameter("prjId");
        BaseLogUtils.newLogger("上海农商银行查询是否有解绑数据开始:prjId={}",prjId).info();
        DataResultVO resultVO = new DataResultVO(BaseConstant.IS_YESNO_YES,"查询解绑信息成功");
        BaseLogUtils.newLogger("上海农商银行根据prjId查询项目信息表Start:prjId={}",prjId).info();
        PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
        BaseLogUtils.newLogger("上海农商银行根据prjId查询项目信息表End:prjId={},prjBscInfoVO={}",prjId,JSON.toJSONString(prjBscInfoVO)).info();
        if(null != prjBscInfoVO){
            //查询绑卡信息
            BaseLogUtils.newLogger("上海农商银行查询卡列表信息开始:prjId={},fundRasInsId={}",prjId,prjBscInfoVO.getFundRasInsId()).info();
            DataResultDTO dataResultDTO = srcbankApiService.queryTiedCardList(prjBscInfoVO.getFundRasInsId(), Long.valueOf(prjId), null);
            BaseLogUtils.newLogger("上海农商银行查询卡列表信息结束:prjId={},fundRasInsId={},queryResultData={}",prjId,prjBscInfoVO.getFundRasInsId(),JSON.toJSONString(resultVO)).info();
            if("1".equals(dataResultDTO.getCode()) && CollectionUtils.isEmpty((List<TiedCardListDTO>)dataResultDTO.getData())){
                resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                resultVO.setInfo("不存在绑卡信息！");
                return resultVO;
            }else if (!"1".equals(dataResultDTO.getCode())){
                resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                resultVO.setInfo(dataResultDTO.getInfo());
            }
        }
        return resultVO;
    }
    /**
     * 上海农商还款卡-解绑
     * @param request
     * @return
     */
    @RequestMapping(value = "/unbindCard.do", method = { RequestMethod.POST })
    @ResponseBody
    public DataResultVO unbindCard(HttpServletRequest request){
        String cardNo = request.getParameter("cardNo");
        String prjId = request.getParameter("prjId");
        try {
            BaseLogUtils.newLogger("上海农商银行根据prjId查询项目信息表Start:prjId={}", prjId).info();
            PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
            BaseLogUtils.newLogger("上海农商银行根据prjId查询项目信息表End:prjId={},prjBscInfoVO={}", prjId, JSON.toJSONString(prjBscInfoVO)).info();
            if (null != prjBscInfoVO) {
                //解绑
                BaseLogUtils.newLogger("上海农商银行解绑银行卡开始:prjId={},fundRasInsId={},cardNo={}", prjId, prjBscInfoVO.getFundRasInsId(), cardNo).info();
                DataResultDTO dataResultDTO = srcbankApiService.unbindCard(prjBscInfoVO.getFundRasInsId(), Long.valueOf(prjId), cardNo);
                BaseLogUtils.newLogger("上海农商银行解绑银行卡结束:prjId={},fundRasInsId={},cardNo={},resultDTO={}", prjId, prjBscInfoVO.getFundRasInsId(), cardNo, JSON.toJSONString(dataResultDTO)).info();
                if (!"1".equals(dataResultDTO.getCode())) {
                    return new DataResultVO(BaseConstant.IS_YESNO_NO, dataResultDTO.getInfo());
                }
//                BaseLogUtils.newLogger("上海农商银行查询卡列表信息开始:prjId={},fundRasInsId={},cardNo={}",prjId,prjBscInfoVO.getFundRasInsId(),cardNo).info();
//                dataResultDTO = srcbankApiService.queryTiedCardList(prjBscInfoVO.getFundRasInsId(), Long.valueOf(prjId), cardNo);
//                BaseLogUtils.newLogger("上海农商银行查询卡列表信息结束:prjId={},fundRasInsId={},queryResultData={}",prjId,prjBscInfoVO.getFundRasInsId(),JSON.toJSONString(dataResultDTO)).info();
//                if(!"1".equals(dataResultDTO.getCode())){
//                    return new DataResultVO(BaseConstant.IS_YESNO_NO, dataResultDTO.getInfo());
//                }
                return new DataResultVO(BaseConstant.IS_YESNO_YES, "解绑成功",dataResultDTO.getData());
            }
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "项目信息不能为空!");
        }catch (Exception e) {
            BaseLogUtils.newLogger("上海农商还款卡-解绑调用接口异常prjId={},cardNo={},currentUserName={}",prjId,cardNo,CurrentThreadContext.getCurrentUserName()).setThrowable(e).error(LogExceptionEnum.API_CODE_0102);
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "解绑失败，系统异常");
        }
    }

    /**
     * 上海农商还款卡-查询是否绑卡
     * @param request
     * @return
     */
//    @RequestMapping(value = "/queryBindCard.do", method = { RequestMethod.GET })
//    @ResponseBody
//    public DataResultVO queryBindCard(HttpServletRequest request){
//        String prjId = request.getParameter("prjId");
//        DataResultVO resultVO = new DataResultVO(BaseConstant.IS_YESNO_YES,"查询成功");
//        BaseLogUtils.newLogger("上海农商银行根据prjId查询项目信息表Start:prjId={}",prjId).info();
//        PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
//        BaseLogUtils.newLogger("上海农商银行根据prjId查询项目信息表End:prjId={},prjBscInfoVO={}",prjId,JSON.toJSONString(prjBscInfoVO)).info();
//        if(null != prjBscInfoVO){
//            //查询绑卡信息
//            BaseLogUtils.newLogger("上海农商银行查询卡列表信息开始:prjId={},fundRasInsId={}",prjId,prjBscInfoVO.getFundRasInsId()).info();
//            DataResultDTO dataResultDTO = srcbankApiService.queryTiedCardList(prjBscInfoVO.getFundRasInsId(), Long.valueOf(prjId), null);
//            BaseLogUtils.newLogger("上海农商银行查询卡列表信息结束:prjId={},fundRasInsId={},queryResultData={}",prjId,prjBscInfoVO.getFundRasInsId(),JSON.toJSONString(resultVO)).info();
//            if("0".equals(dataResultDTO.getCode())){
//                return new DataResultVO(BaseConstant.IS_YESNO_NO,dataResultDTO.getInfo());
//            }
//
//            if("1".equals(dataResultDTO.getCode())
//                    && CollectionUtils.isNotEmpty((List<TiedCardListDTO>)dataResultDTO.getData())){
//                resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
//                resultVO.setInfo("该用户已绑卡，请解绑后再重新进行绑卡操作！");
//                return resultVO;
//            }
//        }
//        return resultVO;
//    }
    /**
     * 上海农商绑卡
     * @param request
     * @return
     */
    @RequestMapping(value = "/bindCard.do", method = { RequestMethod.GET })
    public ModelAndView bindCard(HttpServletRequest request){
        String prjId = request.getParameter("prjId");
        ModelAndView view = new ModelAndView("institution/snbank/bindCardSHNS");
        BaseLogUtils.newLogger("【上海农商银行】资管绑卡开始:prjId={}",prjId).info();
        BaseLogUtils.newLogger("上海农商银行根据prjId查询项目信息表Start:prjId={}",prjId).info();
        PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
        BaseLogUtils.newLogger("上海农商银行根据prjId查询项目信息表End:prjId={},prjBscInfoVO={}",prjId,JSON.toJSONString(prjBscInfoVO)).info();
        if(null != prjBscInfoVO){
            view.addObject("prjId", prjId);//项目id
            view.addObject("cstId", prjBscInfoVO.getCstId());//客户id
            view.addObject("cstNm", prjBscInfoVO.getCstNm());//客户名称
            view.addObject("certNo", prjBscInfoVO.getCertNo());//身份证号
            view.addObject("lsCntNo", prjBscInfoVO.getLsCntNo());//合同号
            view.addObject("fundRasInsId",prjBscInfoVO.getFundRasInsId());//机构id
            PrjCstAccInfoQueryCondition condition = new PrjCstAccInfoQueryCondition();
            condition.setPrjId(prjBscInfoVO.getId());
            condition.setIsDel(BaseConstant.IS_YESNO_NO);
            condition.setInsCode(prjBscInfoVO.getInsCode());
            BaseLogUtils.newLogger("上海农商银行查询客户账户信息表Start:prjId={},condition={}",prjId,JSON.toJSONString(condition)).info();
            List<PrjCstAccInfoVO> prjCstAccInfoVOS = prjCstAccInfoService.searchList(condition);
            BaseLogUtils.newLogger("上海农商银行查询客户账户信息表End:prjId={},prjCstAccInfoVOS={}",prjId,JSON.toJSONString(prjCstAccInfoVOS)).info();
            if (CollectionUtils.isNotEmpty(prjCstAccInfoVOS)) {
                PrjCstAccInfoVO prjCstAccInfoVO = prjCstAccInfoVOS.get(0);
                view.addObject("cstAccInfoId",prjCstAccInfoVO.getId());
//                view.addObject("bkNm", prjCstAccInfoVO.getBkNm()); // 银行名称
//                view.addObject("accNo", prjCstAccInfoVO.getAccNo()); // 银行账号
//                view.addObject("mp", prjCstAccInfoVO.getMp());//银行预留手机号
//                view.addObject("bkCd", prjCstAccInfoVO.getBkCd()); // 银行代码
            }
            // 上海农商银行支持的银行列表
            //查询支持的银行列表
            List<KeyValueVO> list =  new ArrayList<>();
            BaseLogUtils.newLogger("上海农商银行查询开户银行列表开始:prjId={},fundRasInsId={}",prjBscInfoVO.getId(),prjBscInfoVO.getFundRasInsId()).info();
            DataResultDTO supportBankList = shnsbanksInsApiService.getSupportBankList(prjBscInfoVO.getFundRasInsId(), prjBscInfoVO.getId());
            BaseLogUtils.newLogger("上海农商银行查询开户银行列表结束:prjId={},fundRasInsId={},supportBankList={}",prjBscInfoVO.getId(),prjBscInfoVO.getFundRasInsId(),JSON.toJSONString(supportBankList)).info();
            if (supportBankList != null && supportBankList.getData() != null) {
                Map<String,String> map = (Map<String, String>) supportBankList.getData();
                for (Map.Entry<String, String> banks : map.entrySet()) {
                    KeyValueVO kv = new KeyValueVO();
                    kv.setKey(banks.getKey());
                    kv.setValue(banks.getValue());
                    list.add(kv);
                }
            }
            view.addObject("bankList",list);
        }

        return view;
    }

    /**
     * 获取短信验证码
     * @param request
     * @return
     * @throws BaseException
     */
    @RequestMapping(value = "/getVerificationCode.do", method = { RequestMethod.POST })
    @ResponseBody
    public Object getVerificationCode(HttpServletRequest request) throws BaseException {
        BaseLogUtils.newLogger("上海农商银行获取短信验证码Start:request={},currentUserName={}",JSON.toJSONString(request.getParameterMap()), CurrentThreadContext.getCurrentUserName()).info();
        PrjCstAccInfoQueryCondition prjCstAccInfoQC = new PrjCstAccInfoQueryCondition();

        DataResultVO resultVO = null;
        try{
            //1. 组装请求参数
            resultVO = this.packrjCstAccInfo(prjCstAccInfoQC,request);
            if(null != resultVO){
                return resultVO;
            }
            //四要素校验获取验证码
            BaseLogUtils.newLogger("上海农商银行绑卡交易获取验证码获取短信验证码前四要素校验Start:prjId={},prjCstAccInfoQC={}",prjCstAccInfoQC.getPrjId(),JSON.toJSONString(prjCstAccInfoQC)).info();
            resultVO = prjCstAccInfoService.snVerifyModifyPrjAccInfo(prjCstAccInfoQC);
            BaseLogUtils.newLogger("上海农商银行绑卡交易获取验证码获取短信验证码前四要素校验Start:prjId={},resultVO={}",prjCstAccInfoQC.getPrjId(),JSON.toJSONString(resultVO)).info();
            if (BaseConstant.IS_YESNO_YES == resultVO.getSuccess()) {
                Map<String,String> resultMap = (Map<String, String>) resultVO.getData();
                String chkRstCd = "";
                String rstDesc = "";
                String bankCardType = "";
                chkRstCd = resultMap.get("chkRstCd");
                rstDesc = resultMap.get("rstDesc");
                bankCardType = resultMap.get("bankCardType");
                if (chkRstCd.equals("1")) {
                    if ("1".equals(bankCardType)){
                        resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                        resultVO.setInfo("不支持此类银行卡，请更换为借记卡");
                        return resultVO;
                    }
                    BaseLogUtils.newLogger("上海农商银行绑卡交易获取验证码获取短信验证码开始,prjId={},fundRasInsId={},accNo={},mp={}",prjCstAccInfoQC.getPrjId(),prjCstAccInfoQC.getInsId(),prjCstAccInfoQC.getAccNo(),prjCstAccInfoQC.getMp()).info();
                    DataResultDTO dataResultDTO = srcbankApiService.checkShnsBankAddBkcard(prjCstAccInfoQC);
                    BaseLogUtils.newLogger("上海农商银行绑卡交易获取验证码获取短信验证码结束,prjId={},mp={},dataResultDTO={}",prjCstAccInfoQC.getPrjId(),prjCstAccInfoQC.getMp(),JSON.toJSONString(dataResultDTO)).info();
                    if("1".equals(dataResultDTO.getCode())){
                        resultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                        resultVO.setInfo("获取验证码成功");
                        resultVO.setData(dataResultDTO.getData());
                    }else{
                        resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                        resultVO.setInfo(dataResultDTO.getInfo());
                    }
                } else {
                    resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                    resultVO.setInfo(rstDesc);
                }
            }

        }catch (BaseException ex) {
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo(ex.getMessage());
            BaseLogUtils.newLogger("上海农商银行获取短信验证码失败,prjId={},errorResult={}",prjCstAccInfoQC.getPrjId(),ex.getMessage()).info();
            BaseLogUtils.newLogger("上海农商银行获取短信验证码失败,prjId={},errorResult={}",prjCstAccInfoQC.getPrjId(),ex.getMessage()).error(BaseExceptionConstant.INTERFACE_0102);
        } catch (Exception e) {
            BaseLogUtils.newLogger("上海农商银行获取短信验证码失败,prjId={},errorResult={}",prjCstAccInfoQC.getPrjId(),e.getMessage()).info();
            BaseLogUtils.newLogger("上海农商银行获取短信验证码失败,prjId={},errorResult={}",prjCstAccInfoQC.getPrjId(),e.getMessage()).error(BaseExceptionConstant.INTERFACE_0102);
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "获取短信验证码失败，系统异常");
        }
        BaseLogUtils.newLogger("上海农商银行获取短信验证码完成End:prjId={},resultVO={}",prjCstAccInfoQC.getPrjId(),JSON.toJSONString(resultVO)).info();
        return resultVO;
    }

    /**
     * 确认绑卡
     * @param request
     * @return
     * @throws BaseException
     */
    @RequestMapping(value = "/exchangeCardSave.do", method = { RequestMethod.POST })
    @ResponseBody
    public Object exchangeCardSave(HttpServletRequest request) throws BaseException {
        BaseLogUtils.newLogger("上海农商银行确认绑卡Start:request={},currentUserName={}",JSON.toJSONString(request.getParameterMap()), CurrentThreadContext.getCurrentUserName()).info();
        PrjCstAccInfoQueryCondition condition = new PrjCstAccInfoQueryCondition();
        String verificationCode = request.getParameter("verificationCode");
        String uniqueCode = request.getParameter("uniqueCode");

        DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, "四要素校验通过");
        try {
            //1. 组装请求参数
            dataResultVO = this.packrjCstAccInfo(condition,request);
            if(null != dataResultVO){
                return dataResultVO;
            }
            if (StringUtils.isBlank(verificationCode)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "验证码为空!");
            }
            BaseLogUtils.newLogger("上海农商银行确认绑卡修改项目客户账户信息,condition:" + JSONObject.toJSONString(condition)).info();
            dataResultVO = this.prjCstAccInfoService.snVerifyModifyPrjAccInfo(condition);

                if (null != dataResultVO && null != dataResultVO.getData()
                        && BaseConstant.IS_YESNO_YES == dataResultVO.getSuccess()) {
                    Map<String, String> resultMap = (Map<String, String>) dataResultVO.getData();
                    String chkRstCd = resultMap.get("chkRstCd");
                    String rstDesc = resultMap.get("rstDesc");
                    String bankCardType = resultMap.get("bankCardType");
                    //四要素结果
                    if (!BaseConstant.IS_YESNO_YES_STR.equals(chkRstCd)) {
                        dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                        dataResultVO.setInfo(rstDesc);
                        return dataResultVO;
                    }
                    //银行卡类型
                    if (!BaseConstant.IS_YESNO_NO_STR.equals(bankCardType)) {
                        dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                        dataResultVO.setInfo("不支持此类银行卡，请更换为借记卡");
                        return dataResultVO;
                    }
                    DataResultDTO dataResultDTO = srcbankApiService.customerAuth(condition, uniqueCode, verificationCode);
                    if (!"1".equals(dataResultDTO.getCode())) {
                        dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                        dataResultVO.setInfo(dataResultDTO.getInfo());
                        return dataResultVO;
                    }
                    condition.setChkRstCd(chkRstCd);
                    condition.setBankCardTypeCd(bankCardType);
                    BaseLogUtils.newLogger("上海农商银行绑卡成功prjId:{},condition:{}", condition.getPrjId(), JSON.toJSONString(condition)).info();
                    //更新数据
                    condition.setInsCode(PrjBscInfoConstant.PRJ_INS_CODE_SHNS);
                    //绑卡成功
                    DataResultVO resultVO = this.prjCstAccInfoService.modifyPrjAccInfo(condition);
                    dataResultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                    dataResultVO.setInfo("绑卡成功");
                    BaseLogUtils.newLogger("上海农商银行确认绑卡及校验返回dataResultVO=" + JSON.toJSONString(dataResultVO)).info();
                    return dataResultVO;
                }else {
                    dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                    dataResultVO.setInfo(dataResultVO.getInfo());
                    return dataResultVO;
                }
        } catch (BaseException ex) {
            BaseLogUtils.newLogger("上海农商银行确认绑卡及四要素校验异常,prjId:" + condition.getPrjId() + ".ERROR:" + ex.getMessage()).info();
            return new DataResultVO(BaseConstant.IS_YESNO_NO, ex.getMessage());
        } catch (Exception e) {
            BaseLogUtils.newLogger("上海农商银行确认绑卡及四要素校验系统异常prjId={},error={}" ,condition.getPrjId(), e.getMessage()).info();
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "保存失败，系统异常");
        }

    }

    /**
     * 获取验证码及四要素请求参数校验
     * @param prjCstAccInfoQC
     * @param request
     * @return
     */
    private DataResultVO packrjCstAccInfo(PrjCstAccInfoQueryCondition prjCstAccInfoQC, HttpServletRequest request) {

        try{
            //项目客户信息表id
            String cstAccInfoId = request.getParameter("cstAccInfoId");
            //项目id
            String prjId = request.getParameter("prjId");
            //客户id
            String cstId = request.getParameter("cstId");
            //客户名称
            String cstNm = request.getParameter("cstNm");
            //机构id
            String fundRasInsId = request.getParameter("fundRasInsId");
            //证件号码
            String certNo = request.getParameter("certNo");
            //银行名称
            String bkNm = request.getParameter("bkNm");
            //银行代码
            String bkCd = request.getParameter("bkCd");
            //银行卡号
            String accNo = request.getParameter("accNo");
            //银行预留手机号
            String mp = request.getParameter("mp");

            if(StringUtils.isNotEmpty(cstAccInfoId)) {
                prjCstAccInfoQC.setId(Long.valueOf(cstAccInfoId));
            }
            if(StringUtils.isNotEmpty(prjId)) {
                prjCstAccInfoQC.setPrjId(Long.valueOf(prjId));
            }
            if(StringUtils.isNotEmpty(cstId)) {
                prjCstAccInfoQC.setCstId(Long.valueOf(cstId));
            }
            if(StringUtils.isNotEmpty(fundRasInsId)){
                prjCstAccInfoQC.setInsId(Long.valueOf(fundRasInsId));
            }

            prjCstAccInfoQC.setCstNm(cstNm);
            prjCstAccInfoQC.setCertNo(certNo);
            prjCstAccInfoQC.setInsCode(PrjBscInfoConstant.PRJ_INS_CODE_SHNS);
            prjCstAccInfoQC.setCertTypCd(DictionaryConstant.CODE_CERT_TYP_CD_0);//证件类型
            prjCstAccInfoQC.setAccNo(accNo);
            prjCstAccInfoQC.setBkCd(bkCd);
            prjCstAccInfoQC.setBkNo(bkCd);
            prjCstAccInfoQC.setBkNm(bkNm);
            prjCstAccInfoQC.setMp(mp);
            BaseLogUtils.newLogger("上海农商银行绑卡请求参数组装信息完成：prjCstAccInfoQC={}",JSON.toJSONString(prjCstAccInfoQC)).info();
        }catch (BaseException ex) {
            BaseLogUtils.newLogger("上海农商银行绑卡请求参数组装请求参数异常:prjCstAccInfoQC={},Error={}",JSONObject.toJSONString(prjCstAccInfoQC),ex.getMessage()).info();
            BaseLogUtils.newLogger("上海农商银行绑卡请求参数组装请求参数异常:prjCstAccInfoQC={},Error={}",JSONObject.toJSONString(prjCstAccInfoQC),ex.getMessage()).error(BaseExceptionConstant.COMMON_0001);
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "参数请求异常!");
        }
        return null;

    }
}
